﻿using System;
using System.ComponentModel;
using System.Xml.Serialization;
using FlashCards.Dto;
using System.IO;
using System.Collections.Generic;
using System.Windows;

namespace FlashCards.SL.FlashCardCreator.ViewModels
{
    public class LoadingPanelViewModel : BaseViewModel
    {
        protected FlashCardsSilverlightApp Application { get; set; }

        private double _loadingProgress;

        public double LoadingProgress
        {
            get { return _loadingProgress; }
            set { _loadingProgress = value; OnPropertyChanged("LoadingProgress"); }
        }

        bool _isUserDataLoaded;
        bool _isPartTypesCollectionLoaded;
        bool _isCategoriesCollectionLoaded;
        bool _isFlashcardSetLoaded;

        public LoadingPanelViewModel(string endpointName)
        {
            LoadingProgress = 0;
            Application = System.Windows.Application.Current as FlashCardsSilverlightApp;

            //StartLoadUserData();
            LoadData(endpointName);
        }


        void LoadData(string endpointName)
        {
            var proxy = new DataService.SilverlightServiceClient(endpointName);
            StartLoadUserData(proxy);
            StartLoadPartTypes(proxy);
            StartLoadFlashcardSet(proxy);
            StartLoadCategories(proxy);
        }

        void CheckIfDataLoadCompleted()
        {
            if (_isCategoriesCollectionLoaded && _isFlashcardSetLoaded && _isPartTypesCollectionLoaded && _isUserDataLoaded)
                Application.ApplicationDataLoaded();
        }

        protected void StartLoadUserData(DataService.SilverlightServiceClient proxy)
        {
            proxy.GetUserDataCompleted += EndLoadUserData;
            proxy.GetUserDataAsync(Application.UserID.Value);
        }

        protected void EndLoadUserData(object sender, FlashCards.SL.FlashCardCreator.DataService.GetUserDataCompletedEventArgs e)
        {
            LoadingProgress += 20;

            XmlSerializer ser = new XmlSerializer(typeof(User));
            StringReader reader = new StringReader(e.Result);
            var user = ser.Deserialize(reader) as User;

            Application.CurrentUser = user;
            var tmp = e.Result;
            
            _isUserDataLoaded = true;
            CheckIfDataLoadCompleted();
        }

        protected void StartLoadCategories(DataService.SilverlightServiceClient proxy)
        {
            proxy.GetCategoriesCompleted += EndLoadCategories;
            proxy.GetCategoriesAsync();
        }

        protected void EndLoadCategories(object sender, DataService.GetCategoriesCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }

            LoadingProgress += 20;

            XmlSerializer ser = new XmlSerializer(typeof(List<Category>));
            StringReader reader = new StringReader(e.Result);
            var categories = ser.Deserialize(reader) as List<Category>;

            if (categories != null && categories.Count > 0)
            {
                Application.Resources.Add("Categories", categories);
            }

            _isCategoriesCollectionLoaded = true;
            CheckIfDataLoadCompleted();
        }

        protected void StartLoadPartTypes(DataService.SilverlightServiceClient proxy)
        {
            proxy.GetFlashcardPartTypesCompleted += EndLoadPartTypes;
            proxy.GetFlashcardPartTypesAsync();
        }

        protected void EndLoadPartTypes(object sender, FlashCards.SL.FlashCardCreator.DataService.GetFlashcardPartTypesCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }

            LoadingProgress += 20;
            var typesString = e.Result;

            XmlSerializer ser = new XmlSerializer(typeof(List<FlashcardPartType>));
            StringReader reader = new StringReader(typesString);
            var typesList = ser.Deserialize(reader) as List<FlashcardPartType>;

            if (typesList != null && typesList.Count > 0)
            {
                Application.Resources.Add("FlashcardTypeParts", typesList);
            }

            _isPartTypesCollectionLoaded = true;
            CheckIfDataLoadCompleted();
        }

        protected void StartLoadFlashcardSet(DataService.SilverlightServiceClient proxy)
        {
            if (Application.FlashcardSetID.HasValue)
            {
                proxy.GetFlashcardSetCompleted += EndLoadFlashcardSet;
                proxy.GetFlashcardSetAsync(Application.FlashcardSetID.Value);
            }
            else
            {
                _isFlashcardSetLoaded = true;
                CheckIfDataLoadCompleted();
            }
        }

        protected void EndLoadFlashcardSet(object sender, FlashCardCreator.DataService.GetFlashcardSetCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }

            LoadingProgress += 20;
            var flashcardSetString = e.Result;

            var serializer = new XmlSerializer(typeof(FlashcardSet));
            var reader = new StringReader(flashcardSetString);

            var set = serializer.Deserialize(reader) as FlashcardSet;
            if (set != null)
                Application.FlashcardSet = set;

            _isFlashcardSetLoaded = true;
            CheckIfDataLoadCompleted();
        }
    }
}
